/**
 * 
 */
package systole.persistence.brokersDB;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import systole.domain.clinicalInformation.Pathology;
import systole.domain.clinicalInformation.PathologyPatient;
import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.PathologyByPatientBroker;
import systole.view.messages.ErrorMessages;

/**
 * @author jmj
 *
 */
public class PathologyByPatienBrokerDB extends BrokerDB implements
        PathologyByPatientBroker {

    /**
     */
    public PathologyByPatienBrokerDB() {
        super();
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.PathologyByPatientBroker#existPathologyByPatient(systole.domain.clinicalInformation.Pathology, systole.domain.persons.Patient)
     */
    @Override
    public boolean existPathologyByPatient(Pathology pathology, Patient patient)
            throws ExceptionDAO {
        try {
            this.logger.logDebug("exist pathology by patient");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<PathologyPatient> list = currentSession.createCriteria(PathologyPatient.class).add(Restrictions.and(Restrictions.eq("patient", patient), Restrictions.eq("pathology", pathology))).list();
            this.logger.logDebug("exist pathology by patient successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist pathology by patient, msg: " + e.getMessage());
            throw new ExceptionDAO("No se verificar si ya existe la patología del paciente", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.PathologyByPatientBroker#getAllPatientPathologies()
     */
    @Override
    public List<PathologyPatient> getAllPatientPathologies(Patient patient)
            throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting all patient pathology");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<PathologyPatient> list = currentSession.createCriteria(PathologyPatient.class).add(Restrictions.eq("patient", patient)).list();
            this.logger.logDebug("get patient pathology");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all patient pathology, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las patologías del paciente", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.PathologyByPatientBroker#insert(systole.domain.clinicalInformation.PathologyPatient)
     */
    @Override
    public void insert(PathologyPatient pathologyPatient) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving patient pathology");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(pathologyPatient);
            this.logger.logDebug("saved patient pathology");
        } catch (HibernateException e) {
            this.logger.logError("error on save patient pathology, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.PathologyByPatientBroker#update(systole.domain.clinicalInformation.PathologyPatient)
     */
    @Override
    public void update(PathologyPatient pathologyPatient) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating patient pathology");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(pathologyPatient);
            this.logger.logDebug("updated patient pathology");
        } catch (HibernateException e) {
            this.logger.logError("error on update patient pathology, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public void delete(PathologyPatient pathologyPatient) throws ExceptionDAO {
        try {
            this.logger.logDebug("deleting patient pathology");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.delete(pathologyPatient);
            this.logger.logDebug("deleted patient pathology");
        } catch (HibernateException e) {
            this.logger.logError("error on delete patient pathology, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo eliminar", e.fillInStackTrace());
        }
    }
}
